require "Object"
require "World"

-- avoid memory leak
collectgarbage("setpause", 100)
collectgarbage("setstepmul", 5000)

g_winSize = CCDirector:sharedDirector():getWinSize()

--StartScene class
StartScene = Object:new{

}


function StartScene:create(startSceneFile)
	local newStartScene = StartScene:new()
	
	local cc2dNode = CC2DFileCache:sharedCC2DFileCache():addCC2DNode(startSceneFile)
	local ccLayer = tolua.cast(cc2dNode:createCCNode(nil), "CCLayer")
	Misc:setNodeName(ccLayer, "StartScene")
	
	tolua.setpeer(ccLayer, newStartScene)
	
	local function onEnterOrExit(eventType)
		if eventType == kCCNodeOnExit then
			--ccLayer:onExit()
		else
			ccLayer:onEnter()
		end
	end
	
	ccLayer:registerScriptHandler(onEnterOrExit)
	
	if ccLayer:isRunning() then
		ccLayer:onEnter()
	end
	return ccLayer
end
	
	
function StartScene:onEnter()
	Misc:log("World : onEnter .. " .. Misc:getNodeName(self))
	
	local function onTouches(eventType, touches)
		if eventType == CCTOUCHBEGAN then
            --self:onTouchesBegin(touches)
		elseif eventType == CCTOUCHMOVED then
            --self:onTouchesMoved(touches)
		else
            self:onTouchesEnded(touches)
		end
	end
		
    self:registerScriptTouchHandler(onTouches, true, 1, true)
	self:setTouchEnabled(true)
end

function StartScene:onTouchesEnded(touches)
	local world = createWorld()
	local ccTransScene = CCTransitionFlipAngular:create(1, world)
	CCDirector:sharedDirector():replaceScene(ccTransScene)
end

function createStartScene()
	local ccScene = CCScene:create()

	ccScene:addChild(StartScene:create("start_scene.cc2d"))
	return ccScene
end

SimpleAudioEngine:sharedEngine():playBackgroundMusic("background.mp3", true);
SimpleAudioEngine:sharedEngine():pauseBackgroundMusic()

CCDirector:sharedDirector():runWithScene(createStartScene())